//
//  RecommendViewController.swift
//  HiBook
//
//  Created by Apple on 16/8/10.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import WebKit

class RecommendViewController: CommonViewController,WKUIDelegate ,WKNavigationDelegate ,WKScriptMessageHandler  {
    
    var ebookWebView : WKWebView?
    
    var hidden : String?
    
    var tabFrame : CGRect?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setWebView()
        loadWebView()
        self.view.addSubview(ebookWebView!)
        self.navigationController?.navigationBarHidden = true
//        let dateFrom = NSDate(timeIntervalSince1970: 0)
//        WKWebsiteDataStore.defaultDataStore().removeDataOfTypes([WKWebsiteDataTypeCookies,WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeLocalStorage,WKWebsiteDataTypeSessionStorage,WKWebsiteDataTypeOfflineWebApplicationCache], modifiedSince: dateFrom) {
//            
//        }
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func loadWebView(){
        
        let request = NSURLRequest(URL: NSURL(string: "http://\(MAIN_IP):8000/web/indexView.html")!)
        
        self.ebookWebView?.loadRequest(request)
        
    }
    
    func setWebView(){
        
        let config = WKWebViewConfiguration()
        
        self.ebookWebView = WKWebView(frame: self.view.frame ,configuration: config)
        
        self.ebookWebView?.UIDelegate = self
        
        self.ebookWebView?.navigationDelegate = self
        
        self.ebookWebView?.configuration.userContentController.addScriptMessageHandler(self, name: "responesToPhone")
        
        self.ebookWebView?.allowsBackForwardNavigationGestures = true
        
        self.ebookWebView?.scrollView.scrollEnabled = false
        
        
    }
    func responesToPhone(hidden : String){
        print("刷新成功")

        
        switch hidden {
        case "true":
            self.tabBarController?.tabBar.hidden = true
            
        case "false":
            self.tabBarController?.tabBar.hidden = false
        case "center":
            let VC = PersonalViewController()
            
            self.presentViewController(VC, animated: true, completion: { 
                
            })
        case "logout":
            self.dismissViewControllerAnimated(true, completion: { 
                
            })
        case "star":
            self.presentViewController(CollectionViewController(), animated: true, completion: {
                
            })
        case "share":
            self.presentViewController(HappyViewController(), animated: true, completion: {
                
            })
        case "book":
            self.presentViewController(HappyViewController(), animated: true, completion: {
                
            })
        case "wallet":
            self.presentViewController(HappyViewController(), animated: true, completion: {
                
            })
        case "setting":
            self.presentViewController(HappyViewController(), animated: true, completion: {
                
            })
        case "refresh":
            self.ebookWebView?.reload()
            self.tabBarController?.tabBar.hidden = false
        default:
            print("意外错误")
        }
        
    }
    
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        
        NSLog("方法名:\(message.name) 传回:\(message.body)")
        
        self.hidden = message.body as? String
        
        self.responesToPhone(hidden!)
        
        
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
